package rpc

import (
	"time"

	"github.com/wutianze/nats.go"
)

const TIME_OUT = 5 * time.Minute

type Interface interface {
	Invoke() *Request
	ResetTarget(guid string)
	Nats() *nats.Conn
}

type RpcClient struct {
	target string
	client *nats.Conn
}

type Config struct {
	Target  string
	NatsUrl string
}

func NewRpcClient(cfg *Config) (*RpcClient, error) {
	client, err := nats.IConnect(cfg.NatsUrl)
	if err != nil {
		return nil, err
	}

	return &RpcClient{
		client: client,
		target: cfg.Target,
	}, nil
}

func (c *RpcClient) Invoke() *Request {
	return NewRequest(c)
}

func (c *RpcClient) ResetTarget(guid string) {
	c.target = guid
}

func (c *RpcClient) Nats() *nats.Conn {
	return c.client
}
